//
//  LEStoreController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/10.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEStoreController.h"
#import "CollectionCell.h"
#import "LEStoreHeader.h"
#import "LEOrderView.h"
#import "LETodayOrderModel.h"
#import "MakerTools.h"
#import "API.h"
#import <MJRefresh/MJRefresh.h>

#import "LESettingController.h"
#import "LEOrderCenterController.h"
#import "LEMyOrderController.h"
#import "LEStatisticsOrderController.h"
#import "LEStatisticsSaleViewController.h"
#import "LEGoodsListController.h"
#import "LEAddGoodsController.h"
#import "LEStatisticsSevenController.h"
#import <UShareUI/UShareUI.h>
#import "UIViewController+Share.h"

static NSString * const myworkCell = @"myworkCell";

@interface LEStoreController ()<UICollectionViewDelegate,UICollectionViewDataSource>

// 图片数据源
@property(nonatomic,strong)NSArray* imageArray;
// 标题数据源
@property(nonatomic,strong)NSArray* titleArray;
// collectionView
@property(nonatomic,strong)UICollectionView *collectionView;
// <#售票员01#>
@property(nonatomic,strong)LEStoreHeader *headerView;
// <#售票员01#>
@property(nonatomic,strong)LEOrderView *orderView;

@property (nonatomic,strong)MJRefreshNormalHeader *header;


@end

@implementation LEStoreController


- (void)setupUI
{
    [super setupUI];
    //1.导航
    self.title = @"我的店铺";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStyleDone target:self action:@selector(leftShareClick:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    //2.创建 collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemWith = (LEScreenWidth-2)/3;
    layout.itemSize = CGSizeMake(itemWith, itemWith);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    
    CGRect rect = CGRectMake(0,0, LEScreenWidth, LEScreenHeight - 64);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerNib:[UINib nibWithNibName:@"CollectionCell" bundle:nil] forCellWithReuseIdentifier:myworkCell];
    collectionView.contentInset = UIEdgeInsetsMake(170, 0, 0, 0);
    collectionView.backgroundColor = [UIColor whiteColor];
    
    //2.头部
    LEStoreHeader *headerView = (LEStoreHeader *)[UIView getNibView:@"LEStoreHeader"];
    [collectionView addSubview:headerView];
    headerView.frame = CGRectMake(0, -170, LEScreenWidth, 100);
    [headerView.showOrderButton addTarget:self action:@selector(showOrdeClick) forControlEvents:UIControlEventTouchUpInside];
    self.headerView = headerView;
    
    //订单
    LEOrderView *orderView = (LEOrderView *)[UIView getNibView:@"LEOrderView"];
    [collectionView addSubview:orderView];
    orderView.frame = CGRectMake(0, -70, LEScreenWidth, 70);
    [orderView.orderButton addTarget:self action:@selector(ordeCenter) forControlEvents:UIControlEventTouchUpInside];
    self.orderView = orderView;
    
    [self addHeaderRefreshView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController.tabBar setHidden:NO];
}
- (void)addHeaderRefreshView
{
     __weak typeof (self) weakSelf = self;
    self.header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestOrderInfo];
        [self requestSale];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int16_t)(0.2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.header endRefreshing];
        });
    }];
    [self.header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
    [self.header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
    [self.header setTitle:@"正在刷新" forState:MJRefreshStateRefreshing];
    self.header.ignoredScrollViewContentInsetTop = 170;
    self.collectionView.mj_header = self.header;

}
- (void)loadData
{
    NSArray *imageArr = @[@"fa_bu",@"shan_pin",@"din_dan",@"ton_ji",@"setting",@"se_zi"];
    _imageArray = imageArr;
    
    NSArray *titleArr = @[@"发布商品",@"我的商品",@"我的订单",@"订单统计",@"销售统计",@"我的设置"];
    _titleArray = titleArr;
    [_collectionView reloadData];
    
    [self requestOrderInfo];
    [self requestSale];
}
#pragma mark- 网络请求

/**
 请求今日订单
 */
- (void)requestOrderInfo
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"1" forKey:@"flag"];
     __weak typeof (self) weakSelf = self;
    [SVProgressHUD showWithStatus:@"努力加载中..."];
    [API requestAppendKey:StoreOrderUrl params:params success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        LETodayOrderModel *orderModel = [LETodayOrderModel mj_objectWithKeyValues:datas];
        LELog(@"%s---%d---%@",__func__,__LINE__,orderModel.orders_num);
        weakSelf.orderView.model = orderModel;
        
    } falure:^(NSError *er) {
        
    }];
}

/**
 请求七天营业额
 */
- (void)requestSale
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *start = [MakerTools getWeekTimeStamp];
    NSString *end = [MakerTools getCurrentTimestamp];
    
    [params setValue:start forKey:@"start_time"];
    [params setValue:end forKey:@"end_time"];
    
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StoreSaleUrl params:params success:^(NSDictionary *successCode) {
        
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSArray *datas = successCode[@"datas"];
        NSDictionary *dict = datas[0];
        weakSelf.headerView.moneyLabel.text = dict[@"sum"];
        
    } falure:^(NSError *er) {
        
    }];

}

#pragma mark- collectionView 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArray.count;

}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:myworkCell forIndexPath:indexPath];
    NSString *imageStr = _imageArray[indexPath.row];
    cell.icon.image = [UIImage imageNamed:imageStr];
    cell.titleLbl.text = _titleArray[indexPath.row];
    cell.rightLineView.hidden = (indexPath.row + 1)%3 == 0 ? YES : NO;
    return  cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            {
                LELog(@"%s---%d---发布商品",__func__,__LINE__);
                LEAddGoodsController *add = [[LEAddGoodsController alloc] init];
                [self.navigationController pushViewController:add animated:YES];
            }
            break;
        case 1:
            {
                LELog(@"%s---%d---我的商品",__func__,__LINE__);
                LEGoodsListController *goodList = [[LEGoodsListController alloc] init];
                goodList = (LEGoodsListController *) [self getPageController:goodList];
                [self.navigationController pushViewController:goodList animated:YES];
 
            }
            break;
        case 2:
            {
                LELog(@"%s---%d---我的订单",__func__,__LINE__);
                LEMyOrderController *myOrder = [[LEMyOrderController alloc] init];
                [self.navigationController pushViewController:myOrder animated:YES];

            }
            break;
        case 3:
            {
                LELog(@"%s---%d---订单统计",__func__,__LINE__);
                LEStatisticsOrderController *statisticsOrder = [[LEStatisticsOrderController alloc]init];
                [self.navigationController pushViewController:statisticsOrder animated:YES];
            }
            break;
        case 4:
            {
                LELog(@"%s---%d---销售统计",__func__,__LINE__);
                LEStatisticsSaleViewController *statisticsSale = [[LEStatisticsSaleViewController alloc] init];
                [self.navigationController pushViewController:statisticsSale animated:YES];
            }
            break;
        case 5:
            {
                LELog(@"%s---%d---我的设置",__func__,__LINE__);
                LESettingController *setting = [[LESettingController alloc] init];
                [self.navigationController pushViewController:setting animated:YES];
            }
            break;
            
        default:
            break;
    }
}
#pragma mark- 点击事件
- (void)leftShareClick:(UIButton *)sender
{
    LELog(@"%s---%d---点击分享",__func__,__LINE__);
     __weak typeof (self) weakSelf = self;
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        NSDictionary *locationDict = [[NSUserDefaults standardUserDefaults]objectForKey:SAVE_LOCATION];
        NSString *url = locationDict[@"url"];
        if (url == nil) {
            url = @"http://www.baidu.com";
        }
        [weakSelf shareWebPageToPlatformType:platformType goodsUrl:url];
    }];
    
}
- (void)requestInfo
{
    NSString *url = [NSString stringWithFormat:@"%@act=store_goods&op=goods_list",RootUrl];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"1" forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:@"1" forKey:@"type"];
    [API requestAppendKey:url params:dict success:^(NSDictionary *successCode) {
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
    }];

}
- (void)showOrdeClick
{
    LELog(@"%s---%d---展示销售订单",__func__,__LINE__);
    LEStatisticsSevenController *seven = [[LEStatisticsSevenController alloc] init];
    seven.start_time = [MakerTools getDayZeroTimeWith:7];
    seven.end_time = [MakerTools getCurrentTimestamp];
    [self.navigationController pushViewController:seven animated:YES];
}
- (void)ordeCenter
{
    LELog(@"%s---%d---订单中心",__func__,__LINE__);
    LEOrderCenterController *orderCenter = [[LEOrderCenterController alloc] init];
    orderCenter.orderMode = TodayOrder;
    orderCenter =(LEOrderCenterController *) [self getPageController:orderCenter];
    [self.navigationController pushViewController:orderCenter animated:YES];
}
#pragma mark- privateMethod
- (WMPageController *)getPageController:(WMPageController *)controller
{
    controller.menuViewStyle = WMMenuViewStyleLine;
    controller.selectIndex = 0;
    controller.menuHeight = 40;
    controller.titleColorSelected = LESystemBlueColor;
    controller.titleColorNormal = LEHaxColor(0x707070);
    controller.progressWidth = controller.menuItemWidth;
    controller.menuBGColor = [UIColor whiteColor];
    controller.titleSizeSelected = 15;
    return controller;
}

@end




